package com.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author zyf
 */
public class SerializeUtil {
    /**
     * Java 序列化
     * @param object
     * @return
     */
    protected static byte[] serialize(Object object) {
        ByteArrayOutputStream stream= new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(stream);
            oos.writeObject(object);
            oos.close();
            return stream.toByteArray();
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Java 反序列化方法
     * @param bytes
     * @return
     */
    protected static Object deserialize(byte[] bytes){
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
            Object value = ois.readObject();
            ois.close();
            return value;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
